package GUI.Ventas;

import java.rmi.RemoteException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.sale.interfaces.SaleProxyMgt;

public class MenuVentas extends Shell {
	static MenuVentas shell;
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			shell = new MenuVentas(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * @param display
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	public MenuVentas(Display display) throws RemoteException, ProxyException {
		super(display, SWT.SHELL_TRIM);
		setLayout(new FormLayout());
		
		Label lblSeleccioneLaOpcion = new Label(this, SWT.NONE);
		lblSeleccioneLaOpcion.setFont(SWTResourceManager.getFont("Lucida Grande", 13, SWT.NORMAL));
		FormData fd_lblSeleccioneLaOpcion = new FormData();
		fd_lblSeleccioneLaOpcion.left = new FormAttachment(0, 127);
		lblSeleccioneLaOpcion.setLayoutData(fd_lblSeleccioneLaOpcion);
		lblSeleccioneLaOpcion.setText("Seleccione La Opcion Deseada");
		
		Button btnIngresarNuevaVenta = new Button(this, SWT.NONE);
		btnIngresarNuevaVenta.addSelectionListener(new SelectionAdapter() {
			
			public void widgetSelected(SelectionEvent e) {
				MostradorVentaDelivery m = new MostradorVentaDelivery (shell, shell.getStyle(), 0);
			//	shell.setVisible(false);
				m.open();
				//AgregarVenta ag = new AgregarVenta(null,null,0);
				//ag.setVisible(true);
			}
		});
		SaleProxyMgt art = ServiceFacade.getInstance().getSaleProxyMgt();
		//System.out.println("ordenes" + art.getOpenOrders().length);
		
		fd_lblSeleccioneLaOpcion.bottom = new FormAttachment(btnIngresarNuevaVenta, -27);
		FormData fd_btnIngresarNuevaVenta = new FormData();
		btnIngresarNuevaVenta.setLayoutData(fd_btnIngresarNuevaVenta);
		btnIngresarNuevaVenta.setText("Ingresar Nueva Venta");
		
		Button btnAgregarOrdenesA = new Button(this, SWT.NONE);
		btnAgregarOrdenesA.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				MostradorVentaDelivery m = new MostradorVentaDelivery(shell,shell.getStyle(),3);
				//shell.setVisible(false);
				m.open();
				
			}
		});
		fd_btnIngresarNuevaVenta.right = new FormAttachment(btnAgregarOrdenesA, 0, SWT.RIGHT);
		fd_btnIngresarNuevaVenta.bottom = new FormAttachment(btnAgregarOrdenesA, -17);
		fd_btnIngresarNuevaVenta.left = new FormAttachment(btnAgregarOrdenesA, 0, SWT.LEFT);
		FormData fd_btnAgregarOrdenesA = new FormData();
		fd_btnAgregarOrdenesA.top = new FormAttachment(0, 117);
		fd_btnAgregarOrdenesA.left = new FormAttachment(0, 130);
		btnAgregarOrdenesA.setLayoutData(fd_btnAgregarOrdenesA);
		btnAgregarOrdenesA.setText("Agregar Ordenes a una Venta");
		
		Button btnVerVentasAbiertas = new Button(this, SWT.NONE);
		btnVerVentasAbiertas.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				MostradorVentaDelivery a  = new MostradorVentaDelivery (shell, shell.getStyle(), 1);
			//	shell.setVisible(false);
				a.open();
			}
		});
		FormData fd_btnVerVentasAbiertas = new FormData();
		fd_btnVerVentasAbiertas.right = new FormAttachment(btnAgregarOrdenesA, 0, SWT.RIGHT);
		fd_btnVerVentasAbiertas.top = new FormAttachment(btnAgregarOrdenesA, 18);
		fd_btnVerVentasAbiertas.left = new FormAttachment(btnAgregarOrdenesA, 0, SWT.LEFT);
		btnVerVentasAbiertas.setLayoutData(fd_btnVerVentasAbiertas);
		btnVerVentasAbiertas.setText("Ver Ventas Abiertas");
		
		Button btnCerrarVenta = new Button(this, SWT.NONE);
		btnCerrarVenta.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				MostradorVentaDelivery a = new MostradorVentaDelivery(shell,shell.getStyle(),2);
				
				
				
					a.open();
	
			}
		});
		FormData fd_btnCerrarVenta = new FormData();
		fd_btnCerrarVenta.right = new FormAttachment(100, -128);
		fd_btnCerrarVenta.left = new FormAttachment(0, 130);
		fd_btnCerrarVenta.top = new FormAttachment(btnVerVentasAbiertas, 22);
		btnCerrarVenta.setLayoutData(fd_btnCerrarVenta);
		btnCerrarVenta.setText("Cerrar Venta");
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("VENTAS");
		setSize(450, 337);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
